package corpustools;
import swutils.BinaryIn;
import swutils.BinaryOut;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;




/**
 * Simple test program to print out all utf-8 sequences.
 */
public class Defined {

    /** Utility class. No construction needed or allowed. */
    private Defined() { }


    /**
     * Breaks a character into multiple bytes and prints out the byte sequence.
     * @param cp the code point to print
     */
    public static void printTheHardWay(int cp) {
        char[] c = Character.toChars(cp);
        String cStr = new String(c);
        BinaryIn in = null;
        try {
            in = new BinaryIn(new ByteArrayInputStream(cStr.getBytes("UTF-8")));
        } catch (UnsupportedEncodingException e) {
            System.out.println(e);
        }
        StringBuilder sb = new StringBuilder();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BinaryOut out = new BinaryOut(baos);

        while (!in.isEmpty()) {
            byte b = in.readByte();
            sb.append(String.format("%x ", b));
            out.write(b);
        }
        out.flush();
        String bstr = baos.toString();
        System.out.print(bstr);
    }

    /** Max code point for Unicode. */
    private static final int UNICODEMAX = 0x10FFFF;

    /**
     * Print out all of Unicode.
     * @param args unused
     */
    public static void main(String[] args) {
        int counter = 0;
        String block = "";
        for (int i = 0; i < UNICODEMAX; i++) {
            printTheHardWay(i);
            System.out.print(" ");
            final int LineLength = 16;
            if (++counter == LineLength) {
                counter = 0;
                Character.UnicodeBlock b = Character.UnicodeBlock.of(i + 1);
                String newBlock = "";
                if (b == null) {
                    newBlock = "null";
                } else {
                    newBlock = b.toString();
                }
                if (newBlock.equals(block)) {
                    printTheHardWay((int) '\n');
                    continue;
                }
                final int NL = 0x0A;
                printTheHardWay(NL);
                printTheHardWay(NL);
                int j = 0;
                while (j < newBlock.length()) {
                    printTheHardWay((int) newBlock.charAt(j));
                    j++;
                }

                String cStr = new String(Character.toChars(i + 1));
                String byteSeqLength = "";
                try {
                    byteSeqLength =
                        " "
                        + cStr.getBytes("UTF-8").length
                        + " bytes\n";
                } catch (UnsupportedEncodingException e) {
                    System.out.println(e);
                }
                j = 0;
                while (j < byteSeqLength.length()) {
                    printTheHardWay((int) byteSeqLength.charAt(j));
                    j++;
                }
                block = newBlock;
            }
        }
    }
}
